package com.xb.loan.util;




import ognl.Ognl;
import ognl.OgnlContext;

public class OgnlHelper {
    
	public final static String EXPRESSION_PREX = "temp";
	public final static String EXPRESSION_NAME_PREX = "temp.";

	/**
	 * 通过Ognl获取对象属性值，默认的表达式前缀。
	 * @param object
	 * @param valueName
	 * @return
	 * @throws Exception
	 */
	public static Object getValue(Object object,String valueName)throws Exception{
		OgnlContext context = new OgnlContext();
		context.put(EXPRESSION_PREX, object);
		
		Object expression = Ognl.parseExpression(EXPRESSION_NAME_PREX + valueName);
		return Ognl.getValue(expression, context);
	}
	
	
	/**
	 * 通过Ognl设置对象属性值，默认的表达式前缀。
	 * @param object
	 * @param valueName
	 * @param value
	 * @throws Exception
	 */
	public static void setValue(Object object,String valueName,Object value)throws Exception{
		OgnlContext context = new OgnlContext();
		context.put(EXPRESSION_PREX, object);
		
		Object expression = Ognl.parseExpression(EXPRESSION_NAME_PREX + valueName);
		
		Ognl.setValue(expression, context,value);
	}
	
	
}
